package com.four.peoplemanage.holiday.service;

import com.four.peoplemanage.common.bean.Page;
import com.four.peoplemanage.holiday.dao.HolidayDao;
import com.four.peoplemanage.holiday.entity.Holiday;
import com.four.peoplemanage.information.entity.Information;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HolidayService {
    @Autowired
    private HolidayDao holidayDao;

    //查看节假日
    public Page<Holiday> getOnrePage(int pageNum, int pageSize, String holiday_name, String datetm
                                     ){
        //计算起始位置
        int start=(pageNum-1)*pageSize;
        //获取分页的数据
        List<Holiday> list=holidayDao.canHoliday(start,pageSize,holiday_name,datetm);
        //获取总条目数
        int total=holidayDao.countHoliday(holiday_name,datetm);
        //计算总页数
        int pages =(int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1);
        //创建分页对象
        Page<Holiday> page=new Page<>();
        page.setRecords(list);
        page.setTotal(total);
        page.setPages(pages);
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        return page;
    }

    //新增节假日
    public boolean addHoliday(Holiday holiday){
        if (holiday == null){
            return false;
        }
        return holidayDao.insertHoliday(holiday)>0;
    }

    //修改节假日
    public boolean updateHoliday(Holiday holiday){
        if (holiday == null){
            return false;
        }
        return holidayDao.update(holiday)>0;
    }

    //删除节日
    public boolean delHoliday(Integer id){
        if (id == null ){
            return false;
        }
        return holidayDao.delHolidayc(id)>0;
    }

}
